// package-owner/owner-fans/index.js
import { USER_ACTION_TYPE } from '../../../constants/user';
import { PageActionMenu, PageListMixins, UserInfoUpdateMixins, ShareImageMixins } from '../../../behaviors/page';
import { getUserFansList, remarkFansName, getUserExtInfo } from '../../../api/usr';
import { USER_EXT_INFO_SCENE } from '../../../constants/user';
import { tipSuccess } from '../../../common/env';
import { dealFansListData } from '../../../biztools/user';

Page({
    mixins: [PageActionMenu, PageListMixins, UserInfoUpdateMixins, ShareImageMixins],
    data: {
        userExtInfo: {},
        searchValue: '',
        actionFansId: null,
        actionFansRemark: '',
        fansList: [],
        moreActionMenu: [{
            name: '添加备注',
            action: USER_ACTION_TYPE.remarkFansName,
        }],
        showFansRemark: false,
    },
    onShow: async function () {
        // 获取用户额外信息
        const userExtInfo = await getUserExtInfo(USER_EXT_INFO_SCENE.home);
        console.log('OwnerFans => 获取到的用户额外信息为：', userExtInfo);
        this.setData({
            userExtInfo,
        });
    },
    moreFansAction: function (event) {
        console.log('OwnerFans => 粉丝操作：', event.detail);
        this.setData({
            actionFansId: event.detail,
        });
        this.showPageAction();
    },
    // 搜索粉丝
    searchFans: async function (event) {
        console.log('OwnerFans => 搜索的粉丝名为：', event.detail);
        this.setData({
            pageNo: 1,
            searchValue: event.detail,
        }, async () => {
            await this.getListData(true);
        });
    },
    // 选择菜单结果
    onActionSelect: async function (event) {
        console.log('OwnerFans => 选择的菜单为：', event.detail);
        if (event.detail.action === USER_ACTION_TYPE.remarkFansName) {
            this.setData({
                showFansRemark: true,
            });
        }
    },
    // 备注粉丝
    remarkFansAction: async function (event) {
        console.log('OwnerFans => 备注粉丝：', event.detail);
        this.setData({
            showFansRemark: false,
        });
        const { actionFansId, fansList } = this.data;
        const res = await remarkFansName(actionFansId, event.detail);
        if (res) {
            tipSuccess('备注成功');
            this.setData({
                pageNo: 1,
                pageSize: fansList.length,
            }, async () => {
                await this.getListData(true);
            });
        }
    },
    // 获取数据方法
    getListData: async function (isReplace=false) {
        const { pageNo, pageSize, pageTime, searchValue, fansList } = this.data;
        const list = await getUserFansList({
            pageNo,
            pageSize,
            ts: pageTime,
            keyword: searchValue,
        });
        this.setData({
            fansList: isReplace ? dealFansListData(list) : [...fansList, ...dealFansListData(list)],
        });
    },
})